/*
 * 
 * Created on Jul 30, 2005
 */
package com.wgo.precise.client.ui.view.editor.requirementtable;

import java.util.List;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;

import com.wgo.precise.client.ui.model.AbstractPropertySpesification;
import com.wgo.precise.client.ui.model.ClientModelItem;
import com.wgo.precise.client.ui.model.facade.IPropertiesContainer;


/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: TableColumnSorter.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 * 
 */
public class TableColumnSorter extends ViewerComparator {

//    private int index = 0;
    private List<AbstractPropertySpesification> spesifications;
//    private String[] columnNames;
//    private int inversed = 1;

    public TableColumnSorter(List<AbstractPropertySpesification> spesifications, String[] columnNames) {
        super();
        this.spesifications = spesifications;
//        this.columnNames = columnNames;
    }

    /**
     * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
     */
    @Override
    public int compare(Viewer viewer, Object e1, Object e2) {
        Object ee1;
        Object ee2;
        int direction = 1;
        if (e1 instanceof ClientModelItem && e2 instanceof ClientModelItem) {
//            AbstractPropertySpesification id = getPropertySpecification();
            Table table = ((TableViewer)viewer).getTable();
            AbstractPropertySpesification id = spesifications.get((table.getSortColumn().getParent()).indexOf(table.getSortColumn()));

            switch (table.getSortDirection()) {
			case SWT.UP:
				direction = 1 ;
				break;
			case SWT.DOWN:
				direction = -1 ;
				break;
			default:
				direction = 0 ;
				break;
			}
            ee1 = ((IPropertiesContainer) e1).getCurrentPropertyValue(id).getCellEditorValue();
            ee2 = ((IPropertiesContainer) e2).getCurrentPropertyValue(id).getCellEditorValue();
        } else {
            ee1 = e1;
            ee2 = e2;
        }
        return super.compare(viewer, ee1, ee2) * direction;
    }

    /**
     * @see org.eclipse.jface.viewers.ViewerSorter#isSorterProperty(java.lang.Object, java.lang.String)
     */
    @Override
    public boolean isSorterProperty(@SuppressWarnings("unused") Object element, String property) {
//        boolean affected = false;
//        int currentIndex = Arrays.asList(columnNames).indexOf(property);
//        if (this.index == currentIndex) {
//            affected = true;
//        }
//        return affected;
    	return true;
    }
    
//    /**
//     * @param index The index to set.
//     */
//    public void setIndex(int newIndex) {
//    	inversed = this.index == newIndex ? inversed * -1 : 1 ;
//        this.index = newIndex;
//    }
//
//    private AbstractPropertySpesification getPropertySpecification() {
//        return spesifications.get(index);
//    }
}
